<template>
  <el-button type="text" v-bind="$attrs" @click="onClick">
    <slot>删除</slot>
  </el-button>
</template>

<script>
  import { combineUrl, func_ask_for, checkCompUsePosition, isEmptyObj, checkUnSetUrlError } from '../utils'
  import { TYPE_DEL, REQ_METHOD } from '../token'

  export default {
    name: 'CrudTableBtnDel',
    props: {
      tableData: {
        type: Object,
        default: () => ({})
      },
      url: {
        type: String,
        default: ''
      },
      reqEntity: {
        type: Object,
        default: () => ({})
      },
      isQuery: {
        type: Boolean,
        default: false
      }
    },
    inject: ['getTableDatas'],
    created() {
      checkCompUsePosition(isEmptyObj(this.tableData), 'CrudTableBtnDel', 'CrudTableHandler')
    },
    methods: {
      onClick() {
        if(this.url) {
          if(!(checkUnSetUrlError(TYPE_DEL, this.url))) return
        }
        const id = this.tableData?.row?.id
        func_ask_for.call(this, {
          url: this.isQuery ? this.url + '?id=' + id : combineUrl(this.url, id),
          method: REQ_METHOD[TYPE_DEL],
          ...this.reqEntity
        }).then(() => {
          this.getTableDatas(false)
        })
      }
    },
  }
</script>

